var express = require('express');
var router = express.Router();
var mongoose = require('mongoose');
var Goods = require('../models/goods');

// 链接数据库
mongoose.connect('mongodb://123.207.172.26:27018/shop');
mongoose.connection.on('connected',function(){
  console.log("MongoDB connected success.")
})

mongoose.connection.on('error',function (){
  console.log("MongoDB connected fail.")
})

mongoose.connection.on("disconnected",function () {
  console.log("MongoDB connected disconnected")
})

router.get('/list',function (req,res,text){
  // res.send('hello,good list .');
  //
  let page = req.param("page");
  let pageSize = parseInt(req.param("pagesize"));
  let sort = req.param("sort");
  let skip = (page-1) * pageSize;
  let params = {};
  let priceLevel =  req.param("priceLevel");
  let priceGt = '',priceLte = '';
  if (priceLevel != 'all') {
    switch (priceLevel) {
      case '0': priceGt = 0; priceLte = 100;break;
      case '1': priceGt = 100; priceLte = 500;break;
      case '2': priceGt = 500; priceLte = 1000;break;
      case '3': priceGt = 1000; priceLte = 5000;break;
    }

    params = {
      salePrice:{
        $gt:priceGt,
        $lte:priceLte
      }
    }
  }

  let goodsModel = Goods.find(params).skip(skip).limit(pageSize);
  goodsModel.sort({'salePrice':sort});
  goodsModel.exec({},function(err,doc){
    if(err){
      res.json({
        status:'1',
        msg:err.message
      })
    }else {
      res.json({
        status:'0',
        msg:'',
        result:{
          count:doc.length,
          list:doc
        }
      })
    }
  })
})

// 加入到购物车
router.post("/addCart",function(req,res,next){
  var userId = '100000077',productId = req.body.productId;
  // var userId = '100000077',productId = req.param("productId");;
  var User = require('../models/user');

  User.findOne({userId:userId},function(err,userDoc){

    // res.json({
    //   status:"1",
    //   msg:userDoc
    // })

    if (err) {
      res.json({
        status:"1",
        msg:err.message
      })
    }else{
      // console.log("userDoc:"+userDoc);
      if (userDoc) {
        let goodsItem = '';
        userDoc.cartList.forEach(function (item){
          if (item.productId == productId) {
            goodsItem = item;
            item.productNum ++;
          }
        });

        // 判断一下购物车里面是否有商品,如果有只添加数量
        if (goodsItem) {
          userDoc.save(function(err2,doc2){
            if(err){
              res.json({
                status:"1",
                msg:err.message
              })
            }else{

                userDoc.save(function(err2,doc2){
                  res.json({
                    status:0,
                    msg:'',
                    result:'suc'
                  })
                })
            }
          })
        }else{
          Goods.findOne({productId:productId},function(err,doc){
            if(err){
              res.json({
                status:"1",
                msg:err.message
              })
            }else{
              if (doc) {
                doc.productNum = 1;
                doc.checked = 1;
                console.log(doc);
                console.log(User.cartList);
                userDoc.cartList.push(doc);
                userDoc.save(function(err2,doc2){
                  res.json({
                    status:0,
                    msg:'',
                    result:doc
                  })
                })
              }
            }
          })
        }




      }
    }










  })
})
module.exports = router;
